#!/usr/bin/env python
# Copyright (C) 2007 by Xyhthyx <xyhthyx@gmail.com>

# This file is part of Parcelle.

# Parcelle is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# Parcelle is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import pango
import gobject
from control.recent import Recent

# Class to generate and control Parcelle's
# tray icon and menus
class TrayIcon:
    
	def __init__(self, parent):
		
		# Get parent
		self.parent = parent
		self.recent = Recent()
		
		# Create tray icon and connect callbacks
		self.tray = gtk.status_icon_new_from_stock(gtk.STOCK_PASTE)
		self.tray.set_tooltip('Parcelle')
		self.tray.connect('popup-menu', self.show_right_menu)
		self.tray.connect('activate', lambda t:gobject.timeout_add(100, self.show_left_menu))
	
	# Left click menu
	def show_left_menu(self):
		
		menu = gtk.Menu()
		# Restore
		menu_image = gtk.Image()
		menu_item = gtk.ImageMenuItem('_Parcelle')
		menu_image.set_from_stock(gtk.STOCK_PASTE, gtk.ICON_SIZE_MENU)
		menu_item.set_image(menu_image)
		menu_item.connect('activate', lambda m:self.parent.widgets.frmMain.present())
		menu.append(menu_item)
		# ----------
		menu.append(gtk.SeparatorMenuItem())
		
		# Add recent items (or not)
		data = self.recent.get_data()
		if not data:
			# There is no data, making empty entry...
			menu_item = gtk.MenuItem('Empty')
			menu_item.set_sensitive(False)
			menu.append(menu_item)
		else:
			# Pango bold to highlight the item that is currently
			# in the clipboard. Using markup causes errors if clipboard
			# text has any <> characters
			bold = pango.AttrList()
			bold.insert(pango.AttrWeight(pango.WEIGHT_BOLD, 0, -1))
			
			for each in range(0,len(data)):
				item = data[each]
				text = item
				
				if len(item) > 50:
					# Item is too long, parsing to 50 characters
					text = item[:47] + '...'
				
				menu_item = gtk.MenuItem(text)
				menu_item.connect('activate', self.item_selected, each)
				# Modify gtk.MenuItem label properties
				item_label = menu_item.get_children()[0]
				item_label.set_use_underline(False)
				item_label.set_single_line_mode(True)
				if item == self.parent.clipboard_text:
					# Current item is also clipboard, making bold...
					item_label.set_attributes(bold)
				menu.append(menu_item)
		
		# ----------
		menu.append(gtk.SeparatorMenuItem())
		# Clear
		menu_image = gtk.Image()
		menu_item = gtk.ImageMenuItem('_Clear')
		menu_image.set_from_stock(gtk.STOCK_CLEAR, gtk.ICON_SIZE_MENU)
		menu_item.set_image(menu_image)
		menu_item.connect('activate', self.clear)
		menu.append(menu_item)
		
		# Popup the menu
		menu.show_all()
		menu.popup(None, None, None, 1, 0)
		# Return false so the gobject.timeout_add() function is
		# called only once
		return False
	
	# Popup menu callback
	def show_right_menu(self, status_icon, button, activate_time):
		
		menu = gtk.Menu()
		
		# Restore/Hide
		menu_image = gtk.Image()
		if self.parent.widgets.frmMain.get_property('visible'):
			menu_item = gtk.ImageMenuItem('_Hide')
			menu_image.set_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_MENU)
			menu_item.set_image(menu_image)
			menu_item.connect('activate', lambda m:self.parent.widgets.frmMain.hide())
		else:
			menu_item = gtk.ImageMenuItem('_Restore')
			menu_image.set_from_stock(gtk.STOCK_JUMP_TO, gtk.ICON_SIZE_MENU)
			menu_item.set_image(menu_image)
			menu_item.connect('activate', lambda m:self.parent.widgets.frmMain.present())
		menu.append(menu_item)
		# About
		menu_item = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
		menu_item.connect('activate', lambda m:self.parent.on_about_clicked(m))
		menu.append(menu_item)
		# Preferences
		menu_image = gtk.Image()
		menu_item = gtk.ImageMenuItem('_Preferences')
		menu_image.set_from_stock(gtk.STOCK_PREFERENCES, gtk.ICON_SIZE_MENU)
		menu_item.set_image(menu_image)
		menu_item.connect('activate', lambda m:self.parent.on_preferences_clicked())
		menu.append(menu_item)
		# ----------
		menu.append(gtk.SeparatorMenuItem())
		# Quit
		menu_item = gtk.ImageMenuItem(gtk.STOCK_QUIT)
		menu_item.connect('activate', self.parent.on_quit_clicked)
		menu.append(menu_item)
		
		menu.show_all()
		menu.popup(None, None, None, button, activate_time)
	
	# Item selected callback
	def item_selected(self, menu_item, item=None):
		
		text = self.recent.get_item(item)
		self.parent.clipboard.set_text(text)
		self.parent.clipboard_text = text
	
	# About callback
	def about(self, widget):
		
		# This helps prevent multiple instances
		if gtk.grab_get_current():
			return
		
		about = AboutDialog()
		about.run()
	
	# Empty clipboard callback
	def clear(self, widget):
		
		self.parent.clipboard.set_text('')
		self.parent.clipboard_text = ''
		self.parent.bfrClip.delete(self.parent.bfrClip.get_start_iter(), self.parent.bfrClip.get_end_iter())
		self.recent.clear()
		
	# Show exit warning message
	def show_warning(self, amount):
		
		# Prepare to show the message
		if amount < 2:
			message = str(amount) + ' old item will be deleted. Are you sure you want to quit?'
		else:
			message = str(amount) + ' oldest items will be deleted. Are you sure you want to quit?'
		
		dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK_CANCEL, message)
		icon = dialog.render_icon(gtk.STOCK_PASTE, gtk.ICON_SIZE_MENU)
		dialog.set_icon(icon)
		answer = dialog.run()
		dialog.destroy()
		
		# Check if response is good
		if answer == gtk.RESPONSE_OK:
			gtk.main_quit()
